<?php
/*
客户管理
*/
//define('Acache_no','1');
class Con_data extends Acan{
	public $defaultAct='list';
	public $Conread = '用户';
	public $Actread = array('list'=>'所有日志','save'=>'保存','del'=>'删除','delall'=>'删除所有');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_data_user);
		//print_r($this->model);
	}
	//--所有客户
	function Act_list(){
		if(empty($this->atitle)){$this->atitle = '所有用户';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$obj = Afun_acan_new('list');
		$status = array(''=>'所有',0=>0,1=>1,2=>2);
		$key_name = $this->model->key_name;
		$columnrr = Afun_arr_remove($this->model->comment,array('mark'));//初始栏目
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_json = $obj->form_json($this->model);
		$column_sop = Afun_arr_remove($this->model->comment,array('id'));
		$all[''] = '所有';
		$where = $this->search($area_val,$post,$column_sop);

		//分页函数
		if(strlen($post['phone'])>0){
			$where .= " and a.phone = '".$post['phone']."'";
		}
		$con->where = "1".$where;
		$con->select = "a.*";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->order = 'DESC';
		$page->page = '10';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->ofindAll($con);
		if(is_array($list))
		foreach($list as $k=>$v){
			$listjson[$v->$key_name] = $v;
		}
		$listjson = Ajson_encode($listjson);
		$otherurl['host'] = Afun_otherurl('host');
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$info = $this->model->ofindPk($this->id);
		Afun_json($info);
	}
	function search(&$area_val,&$post,$column_sop){
		$datetime = false;
		if($_GET){
			$post = $_GET;
			$area_val = array();
			if($post['status']){
				$where .= " and a.status = '$post[status]'";
			}
			$province = $post['province'];
			$city = $post['city'];
			$dist = $post['dist'];
			if($post['province']){
				$where .= " and a.province = '$province'";
				$area_val[1] = $province;
			}
			if($post['city']){
				$where .= " and a.city = '$city'";
				$area_val[2] = $city;
			}
			if($post['dist']){
				$where .= " and a.dist = '$dist'";
				$area_val[3] = $post['dist'];
			}
			$area_val = Ajson_encode($area_val);
			if(!empty($post['search'])){
				$sop = $post['sop'];
				$search = $post['search'];
				if(strpos($search,'*')===false){
					$search = "='".$search."'";
				}else{
					$search = " LIKE '".str_replace('*','%',$search)."'";
				}
				if($post['sop']!=''){
					$where .= " and a.".$post['sop'].$search;
				}else{
					foreach($column_sop as $k=>$v){
						if(empty($whereop)){
							$whereop = "a.".$k.$search;
						}else{
							$whereop .= " or a.".$k.$search;
						}
					}
					if($whereop){
						$where .= " and (".$whereop.")";
					}
				}
			}
			if(!empty($post['search2'])){
				$sop = $post['sop2'];
				$search2 = $post['search2'];
				if(strpos($search2,'*')===false){
					$search2 = "='".$search2."'";
				}else{
					$search2 = " LIKE '".str_replace('*','%',$search2)."'";
				}
				if($post['sop2']!=''){
					$where .= " and a.".$post['sop2'].$search2;
				}else{
					foreach($column_sop as $k=>$v){
						if(empty($whereop)){
							$whereop = "a.".$k.$search2;
						}else{
							$whereop .= " or a.".$k.$search2;
						}
					}
					if($whereop){
						$where .= " and (".$whereop.")";
					}
				}
			}
		}
		return $where;
	}

	function Act_gets(){
		if(empty($this->atitle)){$this->atitle = '抓取';}
		$pagenum = Afunc_option()->get('Adata_gets_pagenum');
		if($pagenum<100){$pagenum=100;Afunc_option('Adata_gets_pagenum')->set($pagenum,'抓取页面数量');}
		include $this->Atpl();
	}
	function Act_area($level=1,$type=1){
		$cache_name = 'acan_data_area_'.$level.$type;
		//Afun_cache_rm($cache_name);
		$obj = Afun_cache_get($cache_name);
		if(!$obj){
			$marea = Afun_db()->model('acan_data_area');
			if($this->id>0){
				$level = $this->id;
			}
			if($type==1) $obj[1][''] = '所有';
			$data = $marea->query("SELECT name FROM `acan_data_area` WHERE level=1 ORDER BY displayorder ASC")->fetchAll();
			foreach ($data as $key => $val) {
				if($type==1){
					$obj[1][$val['name']] = $val['name'];
				}elseif ($type==2) {
					$obj[1][] = $val['name'];
				}
			}
			if($level>1){
				$data = $marea->query("SELECT a.name as pname,b.name FROM `acan_data_area` a JOIN `acan_data_area` b ON b.upid=a.id WHERE a.level=1 ORDER BY b.displayorder ASC")->fetchAll();
				foreach ($data as $key => $val) {
					if($type==1){
						if(empty($obj[2][$val['pname']])) $obj[2][$val['pname']][''] = '所有';
						$obj[2][$val['pname']][$val['name']] = $val['name'];
					}elseif ($type==2) {
						$obj[2][$val['name']] = $val['pname'];
					}
				}
			}
			if($level>2){
				$data = $marea->query("SELECT a.name as pname,b.name FROM `acan_data_area` a JOIN `acan_data_area` b ON b.upid=a.id WHERE a.level=2 ORDER BY b.displayorder ASC")->fetchAll();
				foreach ($data as $key => $val) {
					if($type==1){
						if(empty($obj[3][$val['pname']])) $obj[3][$val['pname']][''] = '所有';
						$obj[3][$val['pname']][$val['name']] = $val['name'];
					}elseif ($type==2) {
						$obj[3][$val['pname']][] = $val['name'];
					}
				}
			}
			Afun_cache_set($cache_name,$obj,600);//写入缓存
		}
		if($type>1){
			return $obj;
		}
		Afun_json($obj);
	}
	function Act_autoarea(){
		$maxnum = Afunc_option()->get('Adata_autoup_num');
		$times = Afunc_option()->get('Adata_autoup_time');
		$obj = $this->Act_area(3,2);
		$data = $this->model->query("SELECT id,address FROM `acan_data_user` WHERE status=0 LIMIT $maxnum")->fetchAll();
		$city = implode('|',array_keys($obj[2]));
		$num = 0;
		foreach ($data as $key => $val) {
			$save = array();
			preg_match('/.*?('.$city.').*?/',$val['address'],$tmp);
			if($tmp[1]){
				$save['province'] = $obj[2][$tmp[1]];
				$save['city'] = $tmp[1];
				$save['status'] = 2;
				$dist = implode('|',$obj[3][$tmp[1]]);
				preg_match('/.*?('.$dist.').*?/',$val['address'],$tmp2);
				if($tmp2[1]){
					$save['dist'] = $tmp2[1];
				}
			}else{
				$save['status'] = -1;
			}
			$num+=$this->model->updatePK($save,$val['id']);
			unset($save);
		}
		Afun_runtime_html();
		echo "<br>成功更新地区 ".$num." 条";
		if($num==0){
			Afun_refresh('','',5000);
		}else{
			Afun_refresh('','',$times);
		}
	}
	function up_user($obj){
		if($obj->send_phone=='') return 3;
		$data = $this->model->find('phone=\''.$obj->send_phone.'\'');
		$save['type']='卖家';
		if($data){
			$mark = '';
			$save['id'] = $data['id'];
			if($data['name']=='' && $obj->send_name!='') $save['name'] = $obj->send_name;
			elseif($data['name']!=$obj->send_name) $mark .= "名称:".$obj->send_name."\n";
			if($data['address']=='' && $obj->send_address!='') $save['address'] = $obj->send_address;
			elseif($data['address']!=$obj->send_address) $mark .= "地址:".$obj->send_address."\n";
			if($data['tel']=='' && $obj->send_tel!='') $save['tel'] = $obj->send_tel;
			elseif($data['tel']!=$obj->send_tel) $mark .= "电话:".$obj->send_tel."\n";
			if($data['type']!=$save['type']){$mark .= "类型:".$save['type']."\n";unset($save['type']);}
			$save['mark'] = $mark;
		}else{
			$save['name'] = $obj->send_name;
			$save['phone'] = $obj->send_phone;
			$save['address'] = $obj->send_address;
			$save['tel'] = $obj->send_tel;
		}
		$this->model->save($save);unset($save);
		if($obj->recv_phone=='') return 4;
		$data = $this->model->find('phone=\''.$obj->recv_phone.'\'');
		$save['type']='买家';
		if($data){
			$mark = '';
			$save['id'] = $data['id'];
			if($data['name']=='' && $obj->recv_name!='') $save['name'] = $obj->recv_name;
			elseif($data['name']!=$obj->recv_name) $mark .= "名称:".$obj->recv_name."\n";
			if($data['address']=='' && $obj->recv_address!='') $save['address'] = $obj->recv_address;
			elseif($data['address']!=$obj->recv_address) $mark .= "地址:".$obj->recv_address."\n";
			if($data['tel']=='' && $obj->recv_tel!='') $save['tel'] = $obj->recv_tel;
			elseif($data['tel']!=$obj->recv_tel) $mark .= "电话:".$obj->recv_tel."\n";
			if($data['type']!=$save['type']){$mark .= "类型:".$save['type']."\n";unset($save['type']);}
			$save['mark'] = $mark;
		}else{
			$save['name'] = $obj->recv_name;
			$save['phone'] = $obj->recv_phone;
			$save['address'] = $obj->recv_address;
			$save['tel'] = $obj->recv_tel;
		}
		$this->model->save($save);
		return 1;
	}
	function Act_autoup(){
		$where='';
		if($_GET['city']){
			$where = " and recv_address LIKE '%".$_GET['city']."%'";
		}
		$maxnum = Afunc_option()->get('Adata_autoup_num');
		$times = Afunc_option()->get('Adata_autoup_time');
		if($maxnum<10){$maxnum=100;Afunc_option('Adata_autoup_num')->set($maxnum,'数据处理数量');}
		if($times<10){$times=100;Afunc_option('Adata_autoup_time')->set($times,'数据处理周期');}
		$con->select = '*';
		$where = 'status=0'.$where;
		$con->limit = $maxnum;
		$mpost = Afun_db()->model('acan_post_kuaidi');
		$con->where = "send_time >= '20130521' and send_time < '20130701' and ".$where;
		$data = $mpost->ofindAll($con);
		if(!$data){
			$con->where = "send_time >= '20130701' and send_time < '20130711' and ".$where;
			$data = $mpost->ofindAll($con);
		}
		if($data){
			foreach ($data as $k => $v) {
				$status = $this->up_user($v);
				$btsql='';
				if($v->send_time){
					$time1 = date('Y-m-d H:i:s',strtotime($v->send_time)-60);
					$time2 = date('Y-m-d H:i:s',strtotime($v->send_time)+60);
					$bt = "send_time > '$time1' and send_time < '$time2'";
					$btsql = $bt.' and ';
				}
				$mpost->update('status='.$status,$btsql."orderid='$v->orderid'");
			}
			echo "成功更新时间 ".count($data)." 条<br>";
			Afun_refresh('','',$times);
		}else{
			echo "当前没有数据需要处理<br>";
			Afun_refresh('','',5000);
		}
		echo $con->where."<br>";
		Afun_runtime_html();
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一个新用户
				$msg = $this->model->save($savedata);
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_json($msg);
	}
	function Act_error(){
		$savedata = $_POST['save'];
		if(is_array($savedata)){
			foreach($savedata as $i=>$val){
				$this->model->updatePk("status=-1",$val['id']);
			}
		}
		Afun_refresh('成功','referrer');
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afunc_group()->usergroupdel($this->id);
			echo '删除成功';
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			Afunc_group()->usergroupdel($val);
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
}

?>